package com.tedu.aopdemo.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
@Aspect
public class SysoutAspect {
    /*    @Before("execution(public int com.tedu.aopdemo.aop.CalculatorImpl.*(..))")
        public void before(JoinPoint joinPoint){
            System.out.println("target"+joinPoint.getTarget());//获取目标对象
            System.out.println(joinPoint.getSignature().getName());//获取方法名
            System.out.println( "参数是"+Arrays.toString(joinPoint.getArgs()));//获取参数(实参)


          String methodName = joinPoint.getSignature().getName();
          String className = joinPoint.getTarget().getClass().getSimpleName();
          String args = Arrays.toString(joinPoint.getArgs());
            System.out.println("开始调用"+className+"."+methodName+"(),实参为:("+args+")");
        }
        @AfterReturning(value = "execution(public int com.tedu.aopdemo.aop.CalculatorImpl.*(..))",
                returning = "returnValue")
        public void afterReturn(JoinPoint joinPoint,Object returnValue){
            String methodName = joinPoint.getSignature().getName();
            String className = joinPoint.getTarget().getClass().getSimpleName();
            System.out.println("调用"+className+"."+methodName+"()方法结束返回值是："+returnValue);
        }

        @After("execution(public int com.tedu.aopdemo.aop.CalculatorImpl.*(..))")
        public void  after(JoinPoint joinPoint){
            String methodName = joinPoint.getSignature().getName();
            String className = joinPoint.getTarget().getClass().getSimpleName();
            System.out.println("调用"+className+"."+methodName+"()方法完毕");

        }
        @AfterThrowing(value = "execution(public int com.tedu.aopdemo.aop.CalculatorImpl.*(..))",throwing = "e")
        public void afterThrowing(JoinPoint joinPoint ,Exception e){
            String methodName = joinPoint.getSignature().getName();
            String className = joinPoint.getTarget().getClass().getSimpleName();
            System.out.println("调用"+className+"."+methodName+"()方法出现异常");
        }*/
    //所有的切入点通常用异常处理机制处理即可
    @Around("execution(public int com.tedu.aopdemo.aop.CalculatorImpl.*(..))")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        String methodName = joinPoint.getSignature().getName();
        String className = joinPoint.getTarget().getClass().getName();
        String args = Arrays.toString(joinPoint.getArgs());

        //@Before里切入的逻辑写在这里
        System.out.println(className + "." + methodName + "()开始执行,参数为:" + args);

        try {
            //实际方法执行，并接收该方法的返回值
            Object returnValue = joinPoint.proceed();//CalculatorImpl.add()执行。。(实际的方法去执行)

            //@AfterReturning
            System.out.println(className + "." + methodName + "()执行完毕");
            return returnValue;
        } catch (Throwable e) {
            throw new RuntimeException(e);
        } finally {
            System.out.println(className + "." + methodName + "()执行完毕");
        }


    }
}